﻿/*
    This file is generated and updated by Sencha Cmd. You can edit this file as
    needed for your application, but these edits will have to be merged by
    Sencha Cmd when it performs code generation tasks such as generating new
    models, controllers or views and when running "sencha app upgrade".

    Ideally changes to this file would be limited and most work would be done
    in other places (such as Controllers). If Sencha Cmd cannot merge your
    changes and its generated code, it will produce a "merge conflict" that you
    will need to resolve manually.
*/

// DO NOT DELETE - this directive is required for Sencha Cmd packages to work.
//@require @packageOverrides

//<debug>
Ext.require('Ext.MessageBox');
Ext.Loader.setPath({
    'Ext': 'touch/src'
});
//</debug>





Ext.application({
    name: 'U-sai',
	
    launch: function() {
	
		var buttonPlus = Ext.create('Ext.Button', {text: '+'});
		var buttonMinus = Ext.create('Ext.Button', {text: '-'});
		var valueArea;
		var valueInternalTemp;
		var valueExternalTemp;
		var e = new Array();
		var u = new Array();
		var src="localization/locale/touch-fr_FR.js";
		u[0]=1.8;
		
		var size = new Array();
		size[0]=10;
		var i=0;
		
		
		
		
		var buttonAdd = Ext.create('Ext.Button', 
			{
				text: Messages.ADDLAYER,
				listeners: {
					tap: function() {
					//alert("You tapped me");
					
					i++;
					for (var y=0;y<u.length;y++)
					{
						//alert("size: "+size[y]+"valeur u: "+u[y]);
					}
					
					calcU();
					
					
					e[0]= new Ext.Panel({ 
						layout: 'hbox',
						items: [{
							xtype: 'selectfield',
							id: 'sel'+i,
							flex:25,
							label: Messages.CHOIXISOL+i,
							placeHolder: '',
							options: [
								{text: Messages.CHOICE1,  value: '1.800'},
								{text: Messages.CHOICE2, value: '0.700'},
								{text: Messages.CHOICE3,  value: '0.500'},
								{text: Messages.CHOICE4,  value: '0.400'},
								{text: Messages.CHOICE5,  value: '0.210'},
								{text: Messages.CHOICE6,  value: '0.180'},
								{text: Messages.CHOICE7,  value: '0.140'},
								{text: Messages.CHOICE8,  value: '0.059'},
								{text: Messages.CHOICE9,  value: '0.055'},
								{text: Messages.CHOICE10,  value: '0.050'},
								{text: Messages.CHOICE11,  value: '0.047'},
								{text: Messages.CHOICE12,  value: '0.045'},
								{text: Messages.CHOICE13,  value: '0.040'},
								{text: Messages.CHOICE14,  value: '0.035'},
								{text: Messages.CHOICE15,  value: '0.030'},
								{text: Messages.CHOICE16,  value: '0.025'},
								{text: Messages.CHOICE17,  value: '0.021'}
							],
							 listeners: {
								change: function (select, newValue, oldValue) 
								{
									var index = this.getId().substr(3,1); 
									
									u[index] = Ext.getCmp(select.getId()).getValue();
									 //alert(u[i-1]);
									 //alert(u.length);
									calcU();
									
								},
								painted: function(select) {
									var index = this.getId().substr(3,1); 
									
									u[index] = Ext.getCmp(select.getId()).getValue();
									calcU();
										
								}
							}
						},
						{
							xtype: 'button',
							id: 'del'+i,
							iconCls: 'delete', 
							flex:1,
								listeners: {
								tap: function () 
								{
									
									//buttonAdd.remove('sel'+i,true);
									//foo.items.home.remove(panelTest);
									//panelTest.items.remove(e[0], true);
									
									var index = this.getId().substr(3,1); 
									//alert(index);
									//alert("deleted u:"+u[index]+" deleted size"+size[index]);
									//u.splice(index);
									size.splice(index,1, 0);
									
									var removed = u.splice(index, 1, 0);
									
									//alert(removed);
									
									//alert("U: "+  JSON.stringify(u)+" size: "+JSON.stringify(size));
									
									Ext.getCmp('sel'+index).destroy();
									Ext.getCmp('num'+index).destroy();
									Ext.getCmp('del'+index).destroy();
									
									calcU();
									
									
									
								}
							}
							}
						
						]
						
					});
					e[1]= new Ext.Panel({
					items:[{
							layout: 'hbox',
							items: [{
								xtype: 'numberfield',
								flex: 8,
								id:'num'+i,
								value:10,
								label: Messages.TAILLE,
								name: 'width',
								listeners: {
									blur: function (select, newValue, oldValue) 
									{
										if (isNaN(parseInt(Ext.getCmp(select.getId()).getValue())) || parseInt(Ext.getCmp(select.getId()).getValue())<0)
										{
											Ext.Msg.alert(Messages.NOTANUMBER);
											Ext.getCmp(select.getId()).setValue(0);
											var index = this.getId().substr(3,1); 
											size[index]=0;
											calcU();
										}
										else
										{
											var index = this.getId().substr(3,1); 
											size[index] = Ext.getCmp(select.getId()).getValue();
											calcU();
										}
										
										
									},
									painted:function(select) {
										var index = this.getId().substr(3,1); 
										size[index] = Ext.getCmp(select.getId()).getValue();
										calcU();
										
									}
								}
							}]
						}] 
					});
					
					panelTest.add(e[0]);
					panelTest.add(e[1]);
					
					}
				}
			}
		);
		
		
		var labelTextU = Ext.create('Ext.Label', {html: Messages.VALUEU, flex: 1});
		var labelTextUResult = Ext.create('Ext.Label', {html: Messages.VALUEURESULT, flex: 2});
		var labelValueU = Ext.create('Ext.Label', { html: 'My label!', flex: 1});
		var labelValueUResult = Ext.create('Ext.Label', {id:'uValueId', html: 'My label!', flex: 1});
		var labelUnitU = Ext.create('Ext.Label', {html: 'W/(m2 K)', flex: 1});
		var labelUnitUResult = Ext.create('Ext.Label', {html: 'W/(m2 K)', flex: 1});
		
		var labelTextWidth = Ext.create('Ext.Label', {html: Messages.TOTALWIDTH, flex: 1});
		var labelValueWidth = Ext.create('Ext.Label', {html: 'My label!', flex: 1});
		var labelUnitWidth = Ext.create('Ext.Label', {html: Messages.CENTIMETER, flex: 1});
		
		var labelTextArea = Ext.create('Ext.Label', {html: Messages.AREARESULT, flex: 2});
		var numberValueArea = Ext.create('Ext.field.Number', {
			value: '10', 
			flex: 1, 
			id:'valueAreaId', 
			listeners: {
				change: function (select, newValue, oldValue) 
				{
					if (isNaN(parseInt(Ext.getCmp(select.getId()).getValue())) || parseInt(Ext.getCmp(select.getId()).getValue())<0)
					{
						Ext.Msg.alert(Messages.NOTANUMBER);
						Ext.getCmp(select.getId()).setValue(0);
					}
					else
					{
						valueArea = Ext.getCmp(select.getId()).getValue();
						//alert(valueArea);
						calcWatts();
					}									
				}
			}
		});
		var labelUnitArea = Ext.create('Ext.Label', {html: 'm2', flex: 1});
		var labelTextInternalTemp = Ext.create('Ext.Label', {html: Messages.INTERNALTEMP, flex: 2});
		var numberValueInternalTemp = Ext.create('Ext.field.Number', {
			value: '20', 
			flex: 1, 
			id:'valueInternalTempId', 
			listeners: {
				change: function (select, newValue, oldValue) 
				{
					if (isNaN(parseInt(Ext.getCmp(select.getId()).getValue())))
					{
						Ext.Msg.alert(Messages.NOTANUMBER);
						Ext.getCmp(select.getId()).setValue(0);
					}
					else
					{
						valueInternalTemp = Ext.getCmp(select.getId()).getValue();
						calcWatts();
					}
				}
			}
		});
		var labelUnitInternalTemp = Ext.create('Ext.Label', {html: '°C', flex: 1});
		
		var labelTextExternalTemp = Ext.create('Ext.Label', {html: Messages.EXTERNALTEMP, flex: 2});
		var numberValueExternalTemp = Ext.create('Ext.field.Number', {
			value: '-7', 
			flex: 1, 
			id:'valueExternalTempId', 
			listeners: {
				change: function (select, newValue, oldValue) 
				{
					if (isNaN(parseInt(Ext.getCmp(select.getId()).getValue())))
					{
						Ext.Msg.alert(Messages.NOTANUMBER);
						Ext.getCmp(select.getId()).setValue(0);
					}
					else
					{
						valueExternalTemp = Ext.getCmp(select.getId()).getValue();
						calcWatts();
					}
				}
			}
		});
		var labelUnitExternalTemp = Ext.create('Ext.Label', {html: '°C', flex: 1});
		
		var labelTextPower = Ext.create('Ext.Label', {html: Messages.NEEDEDPOWER, flex: 2});
		var labelValuePower = Ext.create('Ext.Label', {html: 'My label!', flex: 1});
		var labelUnitPower = Ext.create('Ext.Label', {html: 'W', flex: 1});
		
		var valueU =0;
		
		
		function calcU()
		{
			var rValues=0;
			var sValues=0;
			for (var i=0; i<u.length;i++)
			{
				//alert(JSON.stringify(u));
				if (u[i]!=0)
				{
				
					rValues += (size[i]/100.0)/u[i];
					sValues+=size[i];
				}
			}
			
			var uValue = 1/ (rValues+0.17);
			uValue=uValue.toFixed(2);
			//alert(uValue);
			labelValueU.setHtml(uValue);
			labelValueWidth.setHtml(sValues);
			labelValueUResult.setHtml(uValue);
			calcWatts();
		}
		
		function calcWatts()
		{
			uValue= Ext.getCmp('uValueId').getHtml();
			valueArea = parseInt(Ext.getCmp('valueAreaId').getValue());
			valueInternalTemp = Ext.getCmp('valueInternalTempId').getValue();
			valueExternalTemp = Ext.getCmp('valueExternalTempId').getValue();
			//alert('uValue: '+uValue+' area:'+valueArea+'  internal temp :'+valueInternalTemp+'  external temp:'+valueExternalTemp);
			var usedPower= uValue*valueArea*(valueInternalTemp-valueExternalTemp);
			usedPower=usedPower.toFixed(2);
			labelValuePower.setHtml(usedPower);
		}
		
		var panelWallWidth = Ext.create('Ext.Panel', {
			layout: 'hbox',
			items: [
			{
				xtype: 'numberfield',
				flex: 8,
				value:10,
				label: Messages.TAILLE,
				name: 'width',
				listeners: 
				{
					blur: function (select, newValue, oldValue) 
					{
					
						if (isNaN(parseInt(Ext.getCmp(select.getId()).getValue())) || parseInt(Ext.getCmp(select.getId()).getValue())<0)
						{
							Ext.Msg.alert(Messages.NOTANUMBER);
							Ext.getCmp(select.getId()).setValue(0);
							size[0]=0;
							calcU();
						}
						else
						{
							size[0] = Ext.getCmp(select.getId()).getValue();
							calcU();
						}
					
					
										
					},
					painted:function(select) {
						size[i] = Ext.getCmp(select.getId()).getValue();
						calcU();
					}
				}
			}]
		});
		
		
		
		var panelIsolChoice = Ext.create('Ext.Panel', 
		{
		
		items: [
					{
						xtype: 'selectfield',
						
						label: Messages.CHOIXISOL,
						id: 'sel',
						placeHolder: ' ',
							options: [
								{text: Messages.CHOICE1,  value: '1.800'},
								{text: Messages.CHOICE2, value: '0.700'},
								{text: Messages.CHOICE3,  value: '0.500'},
								{text: Messages.CHOICE4,  value: '0.400'},
								{text: Messages.CHOICE5,  value: '0.210'},
								{text: Messages.CHOICE6,  value: '0.180'},
								{text: Messages.CHOICE7,  value: '0.140'},
								{text: Messages.CHOICE8,  value: '0.059'},
								{text: Messages.CHOICE9,  value: '0.055'},
								{text: Messages.CHOICE10,  value: '0.050'},
								{text: Messages.CHOICE11,  value: '0.047'},
								{text: Messages.CHOICE12,  value: '0.045'},
								{text: Messages.CHOICE13,  value: '0.040'},
								{text: Messages.CHOICE14,  value: '0.035'},
								{text: Messages.CHOICE15,  value: '0.030'},
								{text: Messages.CHOICE16,  value: '0.025'},
								{text: Messages.CHOICE17,  value: '0.021'}
							],
						
						listeners: {
								change: function (select, newValue, oldValue) 
								{
									u[0] = Ext.getCmp(select.getId()).getValue();
									 //Ext.Msg.alert('getValue()', Ext.getCmp(select.getId()).getValue());
									 //alert(u[i-1]);
									 //alert(u.length);
									//alert(newValue.data.value);
									calcU();
									
								}
							}
					}
				]
		
		});
		
		
		var panelAddWall = Ext.create('Ext.Panel', {
		});
		
		
		
		var panelValueU = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextU, labelValueU, labelUnitU]
		});
		var panelValueWidth = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextWidth, labelValueWidth, labelUnitWidth]
		});
		var panelValueUResult = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextUResult, labelValueUResult, labelUnitUResult]
		});
		
		
		var panelLang = Ext.create('Ext.Panel', {
			
			docked: 'top',
			
            xtype: 'toolbar',
			
			items: [
                {
                    xtype: 'container',
					
                    html: 'Pack: ',
                    style: 'color: #fff; padding: 0 10px; width: 85px;'
                },
                {
                    xtype: 'segmentedbutton',
                    allowDepress: false,
					layout: {
				pack: 'center',
				type: 'hbox'
			},
                    items: [
                        { text: 'Français', id:'btnfrFR', handler: function() { 
							window.history.pushState("object or string", "Title", "/prototype?lang=fr_FR"); 
							location.reload();
						}},
                        { text: 'English', id:'btnenEN', handler: function() {
							window.history.pushState("object or string", "Title", "/prototype?lang=en_EN");
							location.reload();
						}},
                        { text: 'Deutsch', id:'btndeDE',  handler: function() 
						{ 
							window.history.pushState("object or string", "Title", "/prototype?lang=de_DE"); 
							location.reload();
						} }
                    ]
                }
            ]
		});
		
		
		
		var panelValueArea = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextArea, numberValueArea, labelUnitArea]
		});
		var panelValueExternalTemp = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextExternalTemp, numberValueExternalTemp, labelUnitExternalTemp]
		});
		var panelValueInternalTemp = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextInternalTemp, numberValueInternalTemp, labelUnitInternalTemp]
		});
		var panelValuePower = Ext.create('Ext.Panel', {
			layout: 'hbox', 
			items: [labelTextPower, labelValuePower, labelUnitPower]
		});
		
		
		
		
		
		var panelTest = Ext.create('Ext.Panel', {
		layout: 'vbox',
		iconCls : '',
		title : '',
		items: [panelIsolChoice, panelWallWidth]
		});
		
		
		
		/////
		
		
		
		
        var foo= Ext.create("Ext.tab.Panel", {
            fullscreen: true,
            tabBarPosition: 'bottom',
			

            items: [
                {
                    title: Messages.TITLE,
                    iconCls: 'home',
                    cls: 'home',
					id:'home',
					xtype: 'fieldset',
					scrollable:true,
					title: Messages.TITLE,
					items: 
					[
						{
							items: [panelLang,panelIsolChoice, panelWallWidth,panelTest, buttonAdd, panelValueU, panelValueWidth]
						}
					]
					
                },
                {
                    xtype: 'fieldset',
                    title: Messages.RESULTTITLE,
                    iconCls: 'star',
                    displayField: 'title',
					items: [panelValueUResult, panelValueArea, panelValueExternalTemp, panelValueInternalTemp, panelValuePower]

                    
                }
            ]
        });
    }
});